import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import current from '../core/current';
import dataInfo from './core/dataInfo';
import { clientBase } from './core/clientBase';
import { commentType } from '../enums/commentType';
import { emptyData } from '../model/common/emptyData';
import returnModel from '../model/common/returnModel';
import commentData from '../model/comments/commentData';
import dataComment from '../model/comments/dataComment';
import { commentPostType } from '../enums/commentPostType';

class commentClient extends clientBase {
  constructor() {
    super();
  }

  get(type: commentType, oid: string, page: number, sort: number): Promise<returnModel<dataComment>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/v2/reply", `&oid=${oid}&plat=2&pn=${page}&ps=20&sort=${sort}&type=${type}`);
        this.client.getResult(info).then((res) => {
          let data: dataComment = res;
          resolve(this.buildSuccessResult<dataComment>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<dataComment>(e));
      }
    });
  }

  getV2(type: commentType, oid: string, page: number, sort: number, offset: string): Promise<returnModel<dataComment>> {
    return new Promise((resolve) => {
      try {
        let csrf = current.instance.csrf;
        let paginationStr = JSON.stringify({ 'offset': offset });
        let info = new apiInfo("https://api.bilibili.com/x/v2/reply",
          `&oid=${oid}&ps=20&pn=${page}&mode=${sort}&type=${type}&csrf=${csrf}&pagination_str=${paginationStr}`);
        this.client.getResult(info).then((res) => {
          let data: dataComment = res;
          resolve(this.buildSuccessResult<dataComment>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<dataComment>(e));
      }
    });
  }

  getChild(oid: string, type: number, rpid: number, page: number): Promise<returnModel<dataComment>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/v2/reply/main", `&oid=${oid}&plat=2&pn=${page}&ps=10&root=${rpid}&type=${type}`);
        this.client.getResult(info).then((res) => {
          let data: dataComment = res;
          resolve(this.buildSuccessResult<dataComment>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<dataComment>(e));
      }
    });
  }

  send(data: commentData, text: string): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info: dataInfo;
        switch (data.type) {
          case commentPostType.New: {
            info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}`);
            break;
          }
          case commentPostType.ReplyComment: {
            info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}&root=${data.rpid}&parent=${data.rpid}`);
            break;
          }
          case commentPostType.ReplyAt: {
            text = "回复 @" + data.userName + ":" + text;
            info = new dataInfo(`&type=${data.emoteType}&oid=${data.oid}&message=${encodeURIComponent(text)}&root=${data.root}&parent=${data.rpid}`);
            break;
          }
        }
        this.client.postResult("https://api.bilibili.com/x/v2/reply/add", info).then((res) => {
          let data = res.data as emptyData;
          resolve(this.buildSuccessResult<emptyData>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  delete(type: number, oid: string, rpid: number): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`&type=${type}&oid=${oid}&rpid=${rpid}`);
        this.client.postResult("https://api.bilibili.com/x/v2/reply/del", info).then((res) => {
          let data = res.data as emptyData;
          resolve(this.buildSuccessResult<emptyData>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  like(type: commentType, aid: string, rpid: number, action: number): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new dataInfo(`&type=${type}&rpid=${rpid}&oid=${aid}&action=${(action == 0 ? 1 : 0)}`);
        this.client.postResult("https://api.bilibili.com/x/v2/reply/action", info).then((res) => {
          let data = res.data as emptyData;
          resolve(this.buildSuccessResult<emptyData>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }
}
export default new commentClient();